local jieyue = fk.CreateSkill {

  name = "joy_mou__jieyue",

  tags = {  },

}



jieyue:addEffect(fk.EventPhaseStart, {
  name = "joy_mou__jieyue",
  anim_type = "control",
  events = {fk.EventPhaseStart},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(jieyue.name) and target == player and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local _, dat = player.room:askForUseActiveSkill(player, "joy_mou__jieyue_active", "#joy_mou__jieyue-invoke", true)
    if dat then
      event:setCostData(self, dat)
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targetid = event:getCostData(self).targets
    local choice = event:getCostData(self).interaction
    local junling = false
    local target = room:getPlayerById(targetid[1])
    if not target.dead then
      room:drawCards(target,1,jieyue.name)
    end
    if target.dead or player.dead then return false end
    if choice == "joy__jieyue1" then
      local to = room:askForChoosePlayers(player,table.map(room.alive_players, Util.IdMapper),1,1,"#joy__jieyue1-choose",jieyue.name,false)
      if #to > 0 then
        if room:askForSkillInvoke(target,jieyue.name,data,"#joy__junling1::"..to[1]) then
          room:damage({
            from = target,
            to = room:getPlayerById(to[1]),
            damage = 1,
            skillName = jieyue.name,
          })
          junling = true
        end
      end
    elseif choice == "joy__jieyue2" then
      if room:askForSkillInvoke(target,jieyue.name,data,"#joy__junling2") then
        local cards1 = target.player_cards[Player.Hand]
        local cards2 = target.player_cards[Player.Equip]
        if #cards1 > 0 then
          room:throwCard(table.random(cards1),jieyue.name,target)
        end
        if #cards2 > 0 then
          room:throwCard(table.random(cards2),jieyue.name,target)
        end
        junling = true
      end
    elseif choice == "joy__jieyue3" then
      if room:askForSkillInvoke(target,jieyue.name,data,"#joy__junling3") then
        room:setPlayerMark(target,"@@joy__jieyuerecover-turn",1)
        junling = true
      end
    elseif choice == "joy__jieyue4" then
      if room:askForSkillInvoke(target,jieyue.name,data,"#joy__junling4") then
        room:setPlayerMark(target,"@@joy__jieyuecard-turn",1)
        room:addPlayerMark(target, MarkEnum.UncompulsoryInvalidity .. "-turn")
        junling = true
      end
    end
    if not junling and not player.dead then
      player:drawCards(3,jieyue.name)
      room:setPlayerMark(target,"@@joy__jieyuedamage-turn",1)
    elseif not player.dead then
      player:drawCards(1,jieyue.name)
    end
  end,
})

jieyue:addEffect(fk.PreHpRecover, {
  name = "#joy_mou__jieyue_trigger",
  mute = true,
  events = {fk.PreHpRecover,fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      return  player == target and target:getMark("@@joy__jieyuerecover-turn") > 0
    elseif event == fk.DamageInflicted then
      return player == target and target:getMark("@@joy__jieyuedamage-turn") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      return true
    elseif event == fk.DamageInflicted then
      data.damage = data.damage + 1
    end
  end,
})
jieyue:addEffect(fk.DamageInflicted, {
  name = "#joy_mou__jieyue_trigger",
  mute = true,
  events = {fk.PreHpRecover,fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      return  player == target and target:getMark("@@joy__jieyuerecover-turn") > 0
    elseif event == fk.DamageInflicted then
      return player == target and target:getMark("@@joy__jieyuedamage-turn") > 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    if event == fk.PreHpRecover then
      return true
    elseif event == fk.DamageInflicted then
      data.damage = data.damage + 1
    end
  end,
})

jieyue:addEffect("prohibit", {
  name = "#joy__jieyue_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@@joy__jieyuecard-turn") > 0 then
      local cardlist = Card:getIdList(card)
      return #cardlist > 0 and table.every(cardlist, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
  prohibit_response = function(self, player, card)
    if player:getMark("@@joy__jieyuecard-turn") > 0 then
      local cardlist = Card:getIdList(card)
      return #cardlist > 0 and table.every(cardlist, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
})

return jieyue